//
//  IntegralMallVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/21.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class IntegralMallVC: UIViewController, MyIntegralChangNotesDelegate{
    

    @IBOutlet weak var pageSelect: UISegmentedControl!
    @IBOutlet weak var headerView: UIView!
    
    @IBOutlet weak var tableView: UITableView!
    
    
    var MyIntegralView: MyIntegralChangNotes!
    var modelList: [integralMallInfo]? {
        didSet {
            self.tableView.reloadData()
        }
    }
  
    var type: String!//1: 1000积分，2:积分账目， 3:兑换记录
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        self.type = "1"
        let IntegralBtn = UIButton(frame: CGRectMake(0, 0, 25, 25))
        IntegralBtn.setBackgroundImage(UIImage(named: "right_item")!, forState: UIControlState.Normal)
        IntegralBtn.addTarget(self, action: #selector(self.onClickRightItem(_:)), forControlEvents:  UIControlEvents.TouchUpInside)
        let share = UIBarButtonItem(customView: IntegralBtn)
        self.navigationItem.rightBarButtonItem = share
        publicMethod.hiddleExtendCellForTableView(self.tableView)

        DataTool.checkIntegralMall(1, completionHandler:{ (newModel) in
             guard let newDataes = newModel else {
                return
            }

            print(newDataes)
            self.modelList = newDataes
        
            
        })

    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        self.tabBarController?.tabBar.hidden  = true
    }
   
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let count = modelList?.count {
            return count
        }
            return 0
        }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 100.0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("IntegralMallCell", forIndexPath: indexPath) as! IntegralMallcell
        let model = self.modelList![indexPath.row]
        
        cell.haedImage.sd_setImageWithURL(NSURL(string: model.imgsrc))
        cell.shopNameLbl.text = model.name
        cell.moneyLbl.text = String(format: "市场售价:%@元",model.money)
        cell.integralLbl.text = String(format: "兑换积分:%@积分",model.integral)

        return cell
     }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let model = self.modelList![indexPath.row]
        
        let sb = UIStoryboard(name: "MySelf", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("exchangeDetailVC") as! exchangeDetailVC

        vc.id = model.id
        self.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(vc, animated: true)
    }

    @IBAction func changePage(sender: UISegmentedControl) {

        if sender.selectedSegmentIndex == 0
        {
            DataTool.checkIntegralMall(1, completionHandler:{ (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                self.modelList = newDataes
                
            })
            

        }
        else if sender.selectedSegmentIndex == 1
        {
            DataTool.checkIntegralMall(2, completionHandler:{ (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                self.modelList = newDataes
                
            })

        }
        
        
    }
    
   
    
    func onClickRightItem(sender: UIButton){
        sender.selected = !sender.selected
        if sender.selected {
            MyIntegralView = MyIntegralChangNotes.init(frame: kScreen_frame)
            MyIntegralView?.delegate = self
            MyIntegralView?.type = self.type
            self.view.addSubview(MyIntegralView)

        } else {
            MyIntegralView.closeBtnClick()
        }
        
    }
    //MARK:IntegralMallVC
    func integralBtnSelected(){
        self.type = "1"
        
    }
    func integralNumSelected(){
        self.type = "2"
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("IntegralAccountsVC") as! IntegralAccountsVC
        vc.type = "1"
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }
    func changeNotesSlected(){
        self.type = "3"
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("IntegralExchangeRecordVC") as! IntegralExchangeRecordVC
       self.hidesBottomBarWhenPushed = true
    self.navigationController?.pushViewController(vc, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }

}
